// pages/user/pages/bookInfoList/bookInfoList.js
var http = require('../../../../service/http.js');
var util = require('../../../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '', //订单id
    itemid: '', //订单id
    skuid: '', //订单id
    bookInfoArr: [], //预约信息列表
    isCard: false, //是否是次卡
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options) {
      this.setData({
        orderId: options.orderId, //订单id
        itemid: options.itemid, //商品id
        skuid: options.skuid, //skuid
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (app.data.shopInfoTimeOut === true) { //非分享且已经过期，清除店铺信息
      console.log('店铺信息已过期，重新加载 app.checkSession()');
      app.checkSession();
    }
    this.selectBookInfoByids();
  },
  /**
   * 根据订单号、orderId、skuid查询预约信息   
   */
  selectBookInfoByids: function() {
    
    http.getReq("/weixin/appointment/getInfo", {
      orderId: this.data.orderId, //订单id
      itemid: this.data.itemid, //商品id
      skuid: this.data.skuid, //skuid
    }, (res)=> { //回调
      console.log(res);
      let bookInfoArr = res.data;
      for (let i = 0; i < bookInfoArr.length; i++) {
        let createTimeStr = bookInfoArr[i].createTime;
        let y = createTimeStr.slice(0, 4);
        let m = createTimeStr.slice(4, 6);
        let d = createTimeStr.slice(6, 8);
        let h = createTimeStr.slice(8, 10);
        let mm = createTimeStr.slice(10, 12);
        let s = createTimeStr.slice(12, 14);

        bookInfoArr[i].isCard = bookInfoArr[i].eccode.includes('CK');
        bookInfoArr[i].createDate = y + '-' + m + '-' + d;
        bookInfoArr[i].createHourTime = util.formateTimeStr(h, mm, s);

      }

      this.setData({
        bookInfoArr: res.data
      })
    });
  },
  //取消预约
  cancelBook: function(e) {
    console.log('取消预约', e);
    wx.showModal({
      title: '',
      content: '您确定要取消预约么？',
      showCancel: true,
      cancelText: '不取消',
      confirmText: '确定',
      success: function(model) {
        if (model.confirm) {
          http.getReq("/weixin/appointment/cancelAppointment", {
            id: e.currentTarget.dataset.id,
            rdSession: wx.getStorageSync('rd_session') || ''
          }, function(res) { //回调
            util.showToast('取消预约成功', 1000, 'success', function() {
              this.onShow();
            })
          }, true, '正在取消预约');
        }
      }
    })

  }
})